//
//  KYRecentSearchTextBasicBehavior.m
//  Haizijiaoyuquan
//
//  Created by Kai Yu on 1/19/14.
//  Copyright (c) 2014 Zhang Kai Yu. All rights reserved.
//

#import "KYRecentSearchTextBasicBehavior.h"

@interface KYRecentSearchTextBasicBehavior ()

@property (nonatomic, strong) UICollisionBehavior *collisionBehavior;
@property (nonatomic, strong) UIPushBehavior *pushBehavior;
@property (nonatomic, strong) UIGravityBehavior *gravityBehavior;

@property (nonatomic, strong) UIDynamicItemBehavior *dynamicItemBehavior;

@end

@implementation KYRecentSearchTextBasicBehavior

- (id)init
{
    self = [super init];
    if (self) {
        [self addChildBehavior:self.collisionBehavior];
        [self addChildBehavior:self.pushBehavior];
        [self addChildBehavior:self.gravityBehavior];
        [self addChildBehavior:self.dynamicItemBehavior];
    }
    return self;
}

- (UIGravityBehavior *)gravityBehavior
{
    if (!_gravityBehavior) {
        _gravityBehavior = [[UIGravityBehavior alloc] initWithItems:nil];
        _gravityBehavior.gravityDirection = CGVectorMake(-1, 0);
        //_gravityBehavior.angle = M_PI;
    }
    return _gravityBehavior;
}

- (UIPushBehavior *)pushBehavior
{
    if (!_pushBehavior) {
        _pushBehavior = [[UIPushBehavior alloc] initWithItems:nil mode:UIPushBehaviorModeInstantaneous];
        _pushBehavior.pushDirection = CGVectorMake(-1, 0);
    }
    return _pushBehavior;
}

- (UICollisionBehavior *)collisionBehavior
{
    if (!_collisionBehavior) {
        _collisionBehavior = [[UICollisionBehavior alloc] initWithItems:nil];
        //_collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
        _collisionBehavior.collisionMode = UICollisionBehaviorModeEverything;
    }
    return _collisionBehavior;
}

- (UIDynamicItemBehavior *)dynamicItemBehavior
{
    if (!_dynamicItemBehavior) {
        _dynamicItemBehavior = [[UIDynamicItemBehavior alloc] initWithItems:nil];
        _dynamicItemBehavior.elasticity = 0.8;
        _dynamicItemBehavior.friction = 0.3;
        _dynamicItemBehavior.allowsRotation = YES;
        _dynamicItemBehavior.resistance = 0.75;
    }
    return _dynamicItemBehavior;
}

- (void)updateFrameUsingRect:(CGRect)frame
{
    [_collisionBehavior removeBoundaryWithIdentifier:@"left"];
    [_collisionBehavior addBoundaryWithIdentifier:@"left" fromPoint:CGPointMake(0, 0) toPoint:CGPointMake(0, frame.size.height)];
    [_collisionBehavior removeBoundaryWithIdentifier:@"top"];
    [_collisionBehavior addBoundaryWithIdentifier:@"top" fromPoint:CGPointMake(0, 0) toPoint:CGPointMake(frame.size.width, 0)];
    [_collisionBehavior removeBoundaryWithIdentifier:@"bottom"];
    [_collisionBehavior addBoundaryWithIdentifier:@"bottom" fromPoint:CGPointMake(0, frame.size.height) toPoint:CGPointMake(frame.size.width, frame.size.height)];
}

- (void)addItem:(id<UIDynamicItem>)item
{
    [self.collisionBehavior addItem:item];
    [self.pushBehavior addItem:item];
    [self.gravityBehavior addItem:item];
    [self.dynamicItemBehavior addItem:item];
}

- (void)removeItem:(id<UIDynamicItem>)item
{
    [self.collisionBehavior removeItem:item];
    [self.pushBehavior removeItem:item];
    [self.gravityBehavior removeItem:item];
    [self.dynamicItemBehavior removeItem:item];
}


@end
